﻿using System.Net;
using System.Web.UI;
using ReadyApps.Wiki.Templates;

namespace ReadyApps.Wiki.Views
{
    class WikiPageNotFoundView : IWikiView
    {
        #region Variables and Constants

        private WikiControlSettings controlSettings;

        #endregion

        #region Constructor

        public WikiPageNotFoundView(WikiControlSettings wikiControlSettings)
        {
            controlSettings = wikiControlSettings;
        }

        #endregion

        #region Public Methods

        public void BuildControlTree(ControlCollection controls, bool raiseEvents)
        {
            Control container = new Control();

            // Instantiate the template
            ITemplate pageNotFoundTemplate = controlSettings.PageNotFoundTemplate ?? new WikiDefaultPageNotFoundTemplate();
            pageNotFoundTemplate.InstantiateIn(container);

            // Set the status code
            if (raiseEvents)
            {
                controlSettings.OnStatusChange(this, new WikiPageStatusEventArgs(HttpStatusCode.NotFound));
            }

            controls.Clear();
            controls.Add(container);
        }

        #endregion
    }
}
